package com.lazy.demo1;

import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lazy.demo1.handler.GidNetContext;
import com.lazy.lib.net.tcp.client.TcpClient;
import com.lazy.lib.util.ByteUtil;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.util.internal.SystemPropertyUtil;

public class DemoTcpClient {
    private static final Logger logger = LoggerFactory.getLogger(DemoTcpClient.class);
    private static int connCount = 60000;
    //private static int connCount = 1;
    private static int poolSize = 100;
    
    
    public static void main(String[] args) {
        String host = "192.168.1.178";
        String localHost = SystemPropertyUtil.get("chost"); // define -Dchost=xx.xx.xx.xx
        int port = 8023;
        if (args.length == 2) {
            host = args[0];
            port = Integer.valueOf(args[1]);
        }
        
        logger.info("chost={}", localHost);
        ExecutorService executor = Executors.newFixedThreadPool(poolSize);
        TcpClient bc = new TcpClient(host, port);
        GidNetContext netContext = new GidNetContext();
        bc.setNetContext(netContext);
        bc.init();
        if (localHost != null) {
            bc.setLocalHost(localHost);
        }
        
        for (int i = 0; i < connCount; i++) {
            executor.submit(new Task(bc));
        }
        
        ScheduledExecutorService scheduledExecutor  = Executors.newScheduledThreadPool(8);
        scheduledExecutor.scheduleAtFixedRate(new SendTask(bc.getMapChannels().values()), 0, 10, TimeUnit.SECONDS);
    }
    
    static class Task implements Runnable {
        TcpClient client;
        
        public Task(TcpClient client){
            this.client = client;
        }

        @Override
        public void run(){
            client.connect();
            //ChannelFuture future = bc.connect().awaitUninterruptibly();
            //System.out.println("future is done." + future.isDone());
        }
    }
    
    static class SendTask implements Runnable {
        Collection<Channel> chCollection;
        
        public SendTask(Collection<Channel> channels){
            this.chCollection = channels; 
        }

        @Override
        public void run(){
            logger.info("Current thread = {} and total threads = {}", Thread.currentThread(), Thread.activeCount());
            for (Channel ch : chCollection) {
                ch.writeAndFlush(Unpooled.wrappedBuffer(getCanPack()));
//                ch.writeAndFlush(
//                        "100bytettttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
//                        + "100bytettttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
//                        + "\r\n");
            }
        }
        
    }
    
    /** 测试车辆信息 */
    private static byte[] getCanPack() {
        StringBuilder sb = new StringBuilder();
        sb.append(" fa fa 46 01 00 00 c2 82 29 3b 00 2c 02 13 1e 0e");
        sb.append(" 04 01 00 ff 44 b8 00 07 01 00 00 06 00 8e 03 4b");
        sb.append(" 00 64 72 1e 00 91 00 00 00 00 20 03 b1 03 ac 02");
        sb.append(" 00 05 5e 28 00 00 65 a8 e0 fb");
        byte[] result = ByteUtil.fromHexString(sb.toString(), " ");
        return result;
    }

}
